package com.dhms.vostok1.data.todo

import com.dhms.vostok1.data.DEFAULT_PAGE_LIMIT
import com.dhms.vostok1.data.DEFAULT_PAGE_OFFSET
import com.dhms.vostok1.data.PrimaryBaseListData
import com.dhms.vostok1.data.PrimaryPageQueryData
import com.dhms.vostok1.utils.AppFeatureManager
import com.dhms.vostok1.utils.ApprovalCenterUrl
import com.squareup.moshi.Json

enum class ToDoCenterState(val value: Int) {
    //待办
    PENDING(1),
    LAUNCH(2),
    OTHER(3),
    ALL(4);

    companion object {
        fun fromInt(type: Int) =
            ToDoCenterState.values().associateBy(ToDoCenterState::value)[type]
                ?: PENDING
    }
}

enum class ToDoBizType(val value: Int) {
    NONE(0),

    //设备采购申请
    DEVICE_PURCHASE_APPLY(1),

    //备件采购申请
    PART_PURCHASE_APPLY(2),

    //设备入库申请
    DEVICE_WAREHOUSE_APPLY(3),

    //备件入库申请
    PART_WAREHOUSE_APPLY(4),

    //检修计划审批
    REPAIR_PLAN_APPROVAL(5),

    //检修任务验收
    REPAIR_TASK_ACCEPTANCE(6),

    //归还申请
    REMAND_APPLY(7),

    //申领申请
    CLAIM_APPLY(8),

    //维保任务
    MAINTENANCE_TASK(9),

    //润滑任务
    LUBRICATION_TASK(10),

    //检修任务
    REPAIR_TASK(11),

    //点巡检任务
    SPOT_PATROL_TASK(12),

    //出库确认
    OUTBOUND_CONFIRMATION(13);

    companion object {
        fun fromInt(type: Int) =
            ToDoBizType.values().associateBy(ToDoBizType::value)[type]
                ?: NONE

        fun stringToInt(name: String): Int {
            ToDoBizType.values().iterator().forEach {
                if (it.toString() == name) {
                    return it.value
                }
            }
            return NONE.value
        }

        fun getAppUrl(type: ToDoBizType, id: String): String? {
            when (type) {
                DEVICE_PURCHASE_APPLY -> {
                    return "${ApprovalCenterUrl.supplyChainDeviceUrl}/$id"
                }
                PART_PURCHASE_APPLY -> {
                    return "${ApprovalCenterUrl.supplyChainDevicePartUrl}/$id"
                }
                DEVICE_WAREHOUSE_APPLY -> {
                    return "${ApprovalCenterUrl.warehouseDeviceUrl}/$id"
                }
                PART_WAREHOUSE_APPLY -> {
                    return "${ApprovalCenterUrl.warehousePartUrl}/$id"
                }
                REPAIR_PLAN_APPROVAL -> {
                    return "${AppFeatureManager.repairPlanDetail}/$id"
                }
                REPAIR_TASK_ACCEPTANCE -> {
                    return "${AppFeatureManager.repairDetailUrl}/$id"
                }
                REMAND_APPLY -> {
                    return "${ApprovalCenterUrl.equipmentReturnDetailUrl}/$id"
                }
                CLAIM_APPLY -> {
                    return "${ApprovalCenterUrl.equipmentRequisitionDetailUrl}/$id"
                }
                REPAIR_TASK -> {
                    return "${AppFeatureManager.repairDetailUrl}/$id"
                }
                OUTBOUND_CONFIRMATION -> {
                    return "${ApprovalCenterUrl.warehouseOutDetailUrl}/$id"
                }
                else -> {
                    return null
                }
            }
        }
    }
}

val BizTaskType: List<ToDoBizType> = listOf(
    ToDoBizType.MAINTENANCE_TASK,
    ToDoBizType.LUBRICATION_TASK,
    ToDoBizType.REPAIR_TASK,
    ToDoBizType.SPOT_PATROL_TASK,
    ToDoBizType.OUTBOUND_CONFIRMATION
)

data class ToDoCenterData(
    //审批/任务单ID
    val id: String? = null,
    @Json(name = "biz_type")
    val bizType: ToDoBizType? = null,
    //待办主题
    val theme: String? = null,
    @Json(name = "updated_at")
    val updatedAt: String? = null,
    @Json(name = "repair_task_edit")
    val repairTaskEdit: Boolean? = null,
    val values: List<ToDoCenterItemValueData>? = null
) {
    val isTask: Boolean
        get() {
            bizType?.let {
                return BizTaskType.contains(it)
            }
            return false
        }
}

data class ToDoCenterItemValueData(
    //排序ID
    val id: Int? = null,
    val name: String? = null,
    val value: String? = null
)

typealias ToDoCenterListResData = PrimaryBaseListData<ToDoCenterData>

data class ToDoCenterListQueryData(
    @Json(name = "matter_status") var matterStatus: Int = ToDoCenterState.PENDING.value,
    override var page: Int = DEFAULT_PAGE_OFFSET,
    @Json(name = "per_page") override val perPage: Int = DEFAULT_PAGE_LIMIT,
    @Json(name = "begin_at") var beginAt: String? = null,
    @Json(name = "end_at") var endAt: String? = null,
    @Json(name = "biz_types") var bizTypes: String? = null

) : PrimaryPageQueryData